package com.fenghao.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.fenghao.subject.application.convert.SubjectCategoryDtoConvert;
import com.fenghao.subject.application.dto.SubjectCategoryDTO;
import com.fenghao.subject.common.entity.Result;
import com.fenghao.subject.domain.entity.SubjectCategoryBO;
import com.fenghao.subject.domain.service.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * SubjectCategory ：题目分类相关！！！
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Resource
    private final SubjectCategoryDomainService subjectCategoryDomainService;


    /**
     * 新增分类
     */
    @PostMapping("/add")
    public Result<Boolean> addSubjectCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.addSubjectCategory.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank((subjectCategoryDTO.getCategoryName())), "分类的名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父级ID不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDTO);
            Boolean addSuccess = subjectCategoryDomainService.addSubjectCategory(subjectCategoryBO);
            return Result.ok(addSuccess);
        } catch (Exception e) {
            log.error("SubjectCategoryController.addSubjectCategory.error:{}", e.getMessage(), e);
            return Result.fail("添加分类失败");
        }
    }


    /**
     * 删除题目分类(软删)
     */
    @PutMapping("/delete")
    public Result<Boolean> deleteSubjectCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {

            // 日志
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.deleteSubjectCategory.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }

            // DTO转BO
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDTO);

            // 与Domain交互
            Boolean result = subjectCategoryDomainService.deleteSubjectCategory(subjectCategoryBO);
            return Result.ok(result);

        } catch (Exception e) {

            log.error("SubjectCategoryController.deleteSubjectCategory.error:{}", e.getMessage(), e);
            return Result.fail("删除题目分类失败");
        }
    }


    /**
     * 更新题目分类
     */
    @PutMapping("/update")
    public Result<Boolean> updateSubjectCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {

            // 日志
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.updateSubjectCategory.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }

            // DTO转BO
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDTO);

            // 与Domain交互
            Boolean result = subjectCategoryDomainService.updateSubjectCategory(subjectCategoryBO);
            return Result.ok(result);

        } catch (Exception e) {

            log.error("SubjectCategoryController.updateSubjectCategory.error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败");
        }
    }


    /**
     * 查询岗位分类
     */
    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDtoConvert.
                    INSTANCE.convertDtoToBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDtoConvert
                    .INSTANCE.convertBoListToDtoList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }


    /**
     * 查询岗位下题目的分类
     */
    @GetMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {
            if (log.isInfoEnabled()) {
                // 序列化入参并打印日志
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父级分类ID不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDtoConvert
                    .INSTANCE.convertBoListToDtoList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }
}
